package com.example.fresheveryday.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.fresheveryday.po.Product;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author ll
 * @since 2023-07-05
 */
@Mapper
public interface ProductMapper extends BaseMapper<Product> {
    //动态查询
    @Select("select * from" + " product ${ew.customSqlSegment} " )
    IPage<Product> getProductByConditions(IPage<Product> page,@Param("ew") Wrapper wrapper);
    //
    @Select("select count(id) from product ${ew.customSqlSegment}")
    int countByConditions(@Param("ew") Wrapper wrapper);
    @Select("select * from product where categoryId = #{id} order by shelfTime desc LIMIT 6")
    List<Product> newRecommend(int id);

    @Select("select * from product where categoryId = #{id} order by viewCount desc LIMIT 4")
    List<Product> getListByCategory(int id);

    @Select("select name from product where id = #{id}")
    Product getNameById(int id);

    @Select("select name,viewCount from product order by viewCount desc limit 5")
    List<Product> getFifthView();

    @Select("update product set saleCount = saleCount + #{quantity} where id = #{id}")
    void addSaleCount(int quantity,int id);

    @Select("update product set quantity = quantity - #{quantity} where id = #{id}")
    void subCount(int quantity,int id);

    @Select("select id,name,imgurl,price,unit,categoryId from product where id = #{productId}")
    List<Product> selectByIdDiy(int id);
}
